//
//  Util.m
//  LocalCache
//
//  Created by tan on 13-2-6.
//  Copyright (c) 2013年 adways. All rights reserved.
//

#import "Util.h"
#import <CommonCrypto/CommonDigest.h>

@implementation Util

+ (NSString *)sha1:(NSString *)str {
    const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:str.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    
    return output;
}

#pragma mark - 三基色
+(UIColor *) stringTOColor:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        return nil;
    }
    unsigned red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 1;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
    range.location = 3;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
    range.location = 5;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
    return color;
}

//*********************写成data文件**************************
+ (void)setData:(id)obj fileName:(NSString *)fileName {
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filename = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    [NSKeyedArchiver archiveRootObject:obj toFile:filename];
}
//********************读取data文件***************************
+ (id)getDataFileName:(NSString *)fileName {
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filename = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
    return dic;
}

+(long long)fileSizeAtPath:(NSString *)fileName {
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *str = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    if([fileManager fileExistsAtPath:str]){
        long long size=[fileManager attributesOfItemAtPath:str error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}
//data转化成json字符串
+ (NSString*)dataToJsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

//警告框
+ (void)showAlertView:(NSString *)title message:(NSString *)message delagate:(id)delagate cancleTitle:(NSString *)cancleTitle otherTitle:(NSString *)otherTitle {
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title message:message delegate:delagate cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle, nil];
    [alertView show];
}

+ (void)saveToUserDefaults:(id)object key:(NSString *)key {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    if (standardUserDefaults) {
        [standardUserDefaults setObject:object forKey:key];
        [standardUserDefaults synchronize];
    }
}

+ (id)getUserDefaultsWithKey:(NSString *)key {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    if (standardUserDefaults) {
        return [standardUserDefaults objectForKey: key];
    }
    return nil;
}

#pragma mark - 屏幕尺寸的适配
+ (CGFloat) getSize:(CGFloat)size {
    
    CGFloat heigth =[UIScreen mainScreen].bounds.size.height;
    float zoom = 1.0f;
    if (heigth == 736) {
        //iPhone6 Plus 逻辑分辨率 414*736
        zoom = (float)1.0f;
    }else if (heigth == 667) {
        //iPhone6  逻辑分辨率 375*667
        zoom = (float)667/736;
    }else if (heigth == 568) {
        //iPhone5S 逻辑分辨率 320*568
        zoom = (float)568/736;
    }else if (heigth == 480) {
        //iPhone 4/4S
        zoom = (float)568/736;
    }
    
    return size/3 * zoom;
}

#pragma mark - 屏幕尺寸的适配
+ (CGFloat)getDoubleSize:(CGFloat)size {
    
    CGFloat heigth =[UIScreen mainScreen].bounds.size.height;
    float zoom = 1.0f;
    if (heigth == 736) {
        //iPhone6 Plus 逻辑分辨率 414*736
        zoom = (float)1.0f;
    }else if (heigth == 667) {
        //iPhone6  逻辑分辨率 375*667
        zoom = (float)667/736;
    }else if (heigth == 568) {
        //iPhone5S 逻辑分辨率 320*568
        zoom = (float)568/736;
    }else if (heigth == 480) {
        //iPhone 4/4S
        zoom = (float)568/736;
    }
    
    return size/2 * zoom;
}

/*如果ui傻逼的按h5的标准标注尺寸，就用这个方法计算size*/
+ (CGFloat)getSbSize:(CGFloat)size {
    size = size*0.5*3;
    CGFloat heigth =[UIScreen mainScreen].bounds.size.height;
    float zoom = 1.0f;
    if (heigth == 736) {
        //iPhone6 Plus 逻辑分辨率 414*736
        zoom = (float)1.0f;
    }else if (heigth == 667) {
        //iPhone6  逻辑分辨率 375*667
        zoom = (float)667/736;
    }else if (heigth == 568) {
        //iPhone5S 逻辑分辨率 320*568
        zoom = (float)568/736;
    }else if (heigth == 480) {
        //iPhone 4/4S
        zoom = (float)568/736;
    }
    
    return size/2 * zoom;

}

//把文件写入沙盒
+ (void)writeFileToCashDict:(NSDictionary *)dict {
    NSString *homepath =NSHomeDirectory();
    NSString  *path = [homepath stringByAppendingPathComponent:@"record.text"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"path++++%@",path);
//    NSArray *array = @[@"yes",@"no"];
    BOOL re = [dict writeToFile:path atomically:YES];
    if (re) {
        NSLog(@"write yes");
    }
}

#pragma mark - 计算label的自适应高度
+ (CGFloat)heightForString:(NSString *)str labelWidth:(CGFloat)labelWidth labelFont:(CGFloat)font {
    
    CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:font] constrainedToSize:CGSizeMake(labelWidth, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    
    return size.height;
}

@end
